package org.he.basic.service.impl;

import org.he.basic.mapper.BaseMapper;
import org.he.basic.query.BaseQuery;
import org.he.basic.service.IBaseService;
import org.he.basic.util.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;

/**
 * @author He
 * @date 2023/11/30  23:55
 */
public class BaseServiceImpl<T> implements IBaseService<T>{
    @Autowired
    private BaseMapper<T> departmentMapper;
    @Override
    public T queryById(Serializable id) {
        return departmentMapper.queryById(id);
    }

    @Override
    public List<T> queryAll() {
        return departmentMapper.queryAll();
    }

    @Override
    public PageInfo<T> queryPage(BaseQuery departmentQuery) {
        // 查询总条数
        long total = departmentMapper.queryTotal(departmentQuery);
        // 查询数据之前应该先判断total为0 如果要用total == 0的话,就需要用到long而不是Long
        if (total == 0) {
            // 返回一个空对象,里面的字段有默认值,默认值为null,返给前端的值为null,不合理,所以应该给定默认值
            return new PageInfo<>();
        }
        // 查询数据
        List<T> list = departmentMapper.queryList(departmentQuery);
        return new PageInfo<>(total, list);
    }

    @Override
    public void batchDelete(List<Long> ids) {
        departmentMapper.batchDelete(ids);
    }

    @Override
    public void insert(T department) {
        departmentMapper.insert(department);
    }

    @Override
    @Transactional
    public void delete(Serializable id) {
        departmentMapper.delete(id);
    }

    @Override
    public void update(T department) {
        departmentMapper.update(department);
    }
}
